package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.R;
import com.sgb.member.databinding.ActivityClientPerformanceBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.entity.ClientPerformanceBean;
import com.sgb.member.ui.adapter.ClientPerformanceAdapter;
import com.sgb.member.view_model.ClientPerformanceModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by：冯齐 on 2021/1/26 16:40
 * Description： 会员业绩
 */
public class ClientPerformanceActivity extends AppActivity implements MemberStatusView.OnChooseListener {

    @Inject
    ClientPerformanceModel viewModel;
    private ActivityClientPerformanceBinding mBinding;
    private String mNo1; // 业务类型
    private String mNo3; // 项目状态
    private CommonPopEntity mAll;

    private ClientPerformanceAdapter mAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, ClientPerformanceActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_client_performance);
        initSearchView();
        initPopup();
        initRecycler();
        loadSearchData(false);
    }

    private void loadSearchData(boolean isLoadMore) {
        viewModel.getPerformances(isLoadMore, mSearchText, mNo1, mNo3);
    }

    /**
     * --------------------------------- 搜索相关 -----------------------------------------------
     */
    private String mSearchText = ""; // 搜索内容

    private void initSearchView() {
        mBinding.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint("请输入公司名称");
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入公司名称");
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadSearchData(false);
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadSearchData(false);
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入企业名称、业绩标题");
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadSearchData(false);
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    /**
     * --------------------------------- Popup相关 -------------------------------------------------
     */

    public void initPopup() {
        mBinding.chooseLayout.setVisibility(View.GONE);
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
        mBinding.popup1.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup3.initPopup(mBinding.grayLayout, mBinding.popLine, this);
    }

    // 获取会员业绩业务类型回调
    public void setMemberPerformanceTypeList(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        mBinding.popup1.setData(data);
        if (isShowPopup) {
            mBinding.popup1.showPop();
        }
    }

    // 项目状态回调
    public void setProjectStatus(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        mBinding.popup3.setData(data);
        if (isShowPopup) {
            mBinding.popup3.showPop();
        }
    }

    // Popup 按照分类加载数据
    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName){
            case "业务类型":
                viewModel.getMemberPerformanceTypeList(true);
                break;
            case "项目状态":
                viewModel.getProjectStatus(true);
                break;
        }
    }

    // Popup Item点击事件
    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String id = position == 0 ? "" : entity.getId();
        switch (mKeyName){
            case "业务类型":
                if (!TextUtils.equals(id, mNo1)) {
                    mNo1 = id;
                    loadSearchData(false);
                }
                break;
            case "项目状态":
                if (!TextUtils.equals(id, mNo3)) {
                    mNo3 = id;
                    loadSearchData(false);
                }
                break;
        }
    }

    /**
     * --------------------------------- recyclerView相关 -------------------------------
     */

    private void initRecycler() {
        TextView textView = mBinding.memberEmptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ClientPerformanceAdapter(this, null);
        mBinding.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<ClientPerformanceBean>() {
            @Override
            public void onItemClick(ClientPerformanceBean memberBean, int position) {
                // 点击业绩信息模块，跳转到企业业绩详情页
                // viewModel.getUrlSetting("achievement", memberBean.getId());
            }
        });
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadSearchData(false);
            }

            @Override
            public void onLoadMore() {
                loadSearchData(true);
            }
        });
    }


    public void showEmpty() {
        mAdapter.clearDatas();
        mBinding.memberEmptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<ClientPerformanceBean> list, boolean isLoadMore, boolean isLastPage) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        if (isLoadMore) {
            mBinding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            mBinding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                mBinding.memberEmptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.memberEmptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
